﻿#include "CCmdSendTips.h"
#include "../cmd/dataprepare.h"
#include "../mainctrl.h"
#include "commmsg.h"
#include "commclass.h"
#include "../act/CEnemyActCityMgr.h"
#include "MsgComm.pb.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_INNER_SEND_TIPS , CCmdSendTips);

int CCmdSendTips::Do(void* pData)
{
    if (pData != NULL)
    {
        CT_WARNING(("inner cmd CCmdSendTips get data"));
        return Done(RET_OK);
    }

    if (pstMainCtrl == NULL)
    {
        CT_ERROR(("mainctrl point is null"));
        return Done(RET_SYSTEM_FAIL);
    }

    if (_uiGID == 0)
    {
        CT_WARNING((""));
        return Done(RET_OK);
    }

    if (_stOfflineTips._astTips.Size() == 0)
    {
        CT_WARNING((""));
        return Done(RET_OK);
    }
    
    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdSendTips::LogicDo(int iRet)
{
    if (iRet)
    {
        CT_WARNING((""));
        return Done(iRet);
    }

    if (pstMainCtrl->m_stPlayerOnline.CheckOnline(_uiGID))
    {
        PBMsgHead stMsgHead;
        stMsgHead.set_time(CMainCtrl::m_pstMain->m_iNow);
        stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);
        stMsgHead.set_msgid(ID_MAIN_TIPS);
        stMsgHead.set_msgret(RET_OK);

        for (int i = 0; i < _stOfflineTips._astTips.Size(); ++i)
        {
            CTips& stTips = _stOfflineTips._astTips[i];
            MsgTips stAns;
            stAns.set_type(stTips._iType);

            switch (stTips._iType)
            {
            case 1: //被加好友
                {
                    MsgFriendTitle* pstMsgFriendTitle = stAns.mutable_from();
                    pstMainCtrl->m_stFriendMgr.GetFriendTitle((unsigned int) stTips._iData1, false, *pstMsgFriendTitle);
                }
                break;
            case 11:   //怪物攻城战10名外奖励
                {
                    stAns.set_bonus_silver(stTips._iData1);
                    stAns.set_bonus_exp(stTips._iData2);
                    stAns.set_bonus_rank(stTips._iData3);
                }
                break;
            case 12: //邀请好友帮忙驱赶地精
            case 13: //好友帮忙驱赶了捣蛋鬼
                {
                    unsigned int uiFriendGID = (unsigned int)stTips._iData1;
                    int iIndex = pstMainCtrl->m_stPlayerSnsCommInfoHash.HashFind(uiFriendGID);
                    if (iIndex < 0)
                    {
                        CT_WARNING(("%u", uiFriendGID));
                        continue;
                    }
                    
                    stAns.set_dest_gid(uiFriendGID);
                    stAns.set_role_name(pstMainCtrl->m_stPlayerSnsCommInfoHash[iIndex]._sRoleName());
                }
                break;
            case 15: //竞技场被挑战失败
                {
                    unsigned int uiAtkGID = (unsigned int)stTips._iData1;
                    int iIndex = pstMainCtrl->m_stPlayerSnsCommInfoHash.HashFind(uiAtkGID);
                    if (iIndex < 0)
                    {
                        CT_WARNING(("%u", uiAtkGID));
                        continue;
                    }

                    stAns.set_role_name(pstMainCtrl->m_stPlayerSnsCommInfoHash[iIndex]._sRoleName());
                    stAns.set_rank(stTips._iData2);
                }
                break;
            case 18:
                {
                    unsigned int uiBeyondMyselfGID = (unsigned int)stTips._iData1;  ////当前竞价最高的用户
                    int iIndex = pstMainCtrl->m_stPlayerSnsCommInfoHash.HashFind(uiBeyondMyselfGID);
                    if (iIndex < 0)
                    {
                        CT_WARNING(("%u", uiBeyondMyselfGID));
                        continue;
                    }
                    stAns.set_role_name(pstMainCtrl->m_stPlayerSnsCommInfoHash[iIndex]._sRoleName());
                    stAns.set_money_type( stTips._iData2 );  //竞拍货币类型, 0:金币, 1:银币
                    stAns.set_last_money( stTips._iData3 ); //上次出价
                }
                break;
            case 19:
                {
                    stAns.set_money_type( stTips._iData1 );  //竞拍货币类型, 0:金币, 1:银币
                    stAns.set_last_money( stTips._iData2 ); //上次出价
                }
                break;
            case 23:  //竞拍成功  
            case 24:  //暗拍成功
                {
                    MsgGridItemInfo * pstItem = stAns.add_bonus();
                    pstItem->set_item_type(stTips._iData1);
                    pstItem->set_item_config_id(stTips._iData2);
                    pstItem->set_item_count(stTips._iData3);
                }
                break;                
            case 66:
                {
                    stAns.set_role_name(pstMainCtrl->m_stRoleMgr.GetRoleName(stTips._iData1));
                    stAns.set_bonus_silver(stTips._iData2);
                }
                break;
            case 67:
                {
                    stAns.set_role_name(pstMainCtrl->m_stRoleMgr.GetRoleName(stTips._iData1));
                }
                break;
            default:
                break;
            }

            CMainCtrl::m_pstMain->m_stEncodeBuf.Clear();
            CMainCtrl::m_pstMain->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
            CMainCtrl::m_pstMain->EncodePBMsg(stAns, pstMainCtrl->m_stEncodeBuf);
            CMainCtrl::m_pstMain->NotifyOnlineUser(_uiGID, pstMainCtrl->m_stEncodeBuf);
        }
    }
    else  
    {
        PREPARE_PLAYER_SNS_DETAIL_INFO(this, _uiGID);

        int iIndex = pstMainCtrl->m_stPlayerSnsDetailInfoHash.HashFind(_uiGID);
        if (iIndex < 0)
        {
            CT_ERROR(("%u", _uiGID));
            return Done(RET_SYS_ERROR);
        }
        CPlayerSnsDetailInfo& stPlayerSnsDetailInfo = pstMainCtrl->m_stPlayerSnsDetailInfoHash[iIndex];

        for (int i = 0; i < _stOfflineTips._astTips.Size(); ++i)
        {
            stPlayerSnsDetailInfo._stMyInfo._stOfflineTips.AddTips(_stOfflineTips._astTips[i]);
        }

        //保存数据
        CVarObjectKey stKey;
        stKey.m_chDataType = DT_PLAYER_SNS_DETAIL_INFO;
        stKey.m_u64ID = stPlayerSnsDetailInfo._uiGID;
        pstMainCtrl->UpdateData(stKey, stPlayerSnsDetailInfo, VOP_UPDATE);
    }

    return Done(RET_OK);
}




